package com.example.administrator.im.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.example.administrator.im.MyApplication;
import com.example.administrator.im.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wp
 * 时间：2016/4/18 11:39
 * 描述：TODO
 */
public class LoginActivity extends Activity {
    @Bind(R.id.et_uid)
    EditText etUid;
    @Bind(R.id.et_password)
    EditText etPassword;

    private String username;
    private String password;
    private MyApplication application;
    private ProgressDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        application = (MyApplication) getApplication();
        etUid.setText("15200000000");
        etPassword.setText("123456");
    }

    @OnClick({R.id.bt_login, R.id.bt_regist})
    public void onClick(View view) {

        username = etUid.getText().toString().trim();
        password = etPassword.getText().toString().trim();

        switch (view.getId()) {
            case R.id.bt_login:
                dialog = ProgressDialog.show(this,null,"正在登陆中");
                if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this,"账号或者密码不能为空",Toast.LENGTH_SHORT).show();
                } else {
                    login();
                }
                break;
            case R.id.bt_regist:
                if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this,"账号或者密码不能为空",Toast.LENGTH_SHORT).show();
                } else {
                    register();
                }
                break;
        }
    }

    private void register() {
       new Thread(new Runnable() {
           @Override
           public void run() {
               // 调用sdk注册方法
               try {
                   EMChatManager.getInstance().createAccountOnServer(username, password);
                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                       }
                   });
               } catch (final EaseMobException e) {
                   runOnUiThread(new Runnable() {
                       //注册失败
                       int errorCode=e.getErrorCode();
                       @Override
                       public void run() {
                           if(errorCode==EMError.NONETWORK_ERROR){
                               Toast.makeText(getApplicationContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
                           }else if(errorCode==EMError.USER_ALREADY_EXISTS){
                               Toast.makeText(getApplicationContext(), "用户已存在！", Toast.LENGTH_SHORT).show();
                           }else if(errorCode==EMError.UNAUTHORIZED){
                               Toast.makeText(getApplicationContext(), "注册失败，无权限！", Toast.LENGTH_SHORT).show();
                           }else{
                               Toast.makeText(getApplicationContext(), "注册失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                           }
                       }
                   });
               }
           }
       }).start();
    }

    private void login() {
        EMChatManager.getInstance().login(username,password,new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        dialog.dismiss();
                        EMGroupManager.getInstance().loadAllGroups();
                        EMChatManager.getInstance().loadAllConversations();
                        Log.d("main", "登陆聊天服务器成功！");
                        //保存用户名
                        application.setUsername(username);
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登陆聊天服务器失败！");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        Toast.makeText(getApplicationContext(), "账号或密码错误！！！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
